﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.金蝶;
using mendian;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.订单.service
{
    public class 订单Service
    {
        public List<orderDTO> orderDTOs { get; set; } = new List<orderDTO>();
        //构造函数 实例化一个对象
        public 订单Service(String shopId, String pageStart, String pageSize)
        {
            //  取接口数据
            this.orderDTOs =
                FunctionHelper.JsonDeserialization<List<orderDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/pmc/getOrderStatusIndexPageable?shopId=" + shopId + "&pageStart=" + pageStart + "&pageSize=" + pageSize
                        )
                    );
            //  数据清洗
            this.订单状态();
        }
        /// <summary>
        /// 每日订单BOM索引
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="dateTimeStr"></param>
        /// <param name="pageStart"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<OrderBomQueryResultDTO> getOrderStatusIndexPageable(String shopId, String dateTimeStr,String pageStart, String pageSize)
        {
            return
                FunctionHelper.JsonDeserialization<List<OrderBomQueryResultDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/bom/getOrderStatusIndexPageable?shopId=" + shopId + "&dateTimeStr=" + dateTimeStr + "&pageStart=" + pageStart + "&pageSize=" + pageSize
                        )
                    );
        }

        /// <summary>
        /// 坚山相关订单一览查询
        /// </summary>
        /// <param name="shopId"></param>
        public static List<orderDTO> getKashiyamaOrderStatusIndexPageable(String shopId)
        {
            //  取接口数据
            List<orderDTO> orderDTOList =
                FunctionHelper.JsonDeserialization<List<orderDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/pmc/getKashiyamaOrderStatusIndexPageable?shopId=" + shopId
                        )
                    );
            //  数据清洗
            foreach (orderDTO orderDTO in orderDTOList)
            {
                if (!String.IsNullOrWhiteSpace(orderDTO.styleSizeCd))
                {
                    try { orderDTO.styleSizeCd = orderDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
            return orderDTOList;
        }
        /// <summary>
        /// 修改订单为已付款或未付款
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static String orderPayment(String orderId, Boolean pay)
        {
            return
                RestCall.httpGetMethod(
                        URL.接口url + "/pmc/orderPayment?orderId=" + orderId + "&pay=" + pay
                    );
        }
        /// <summary>
        /// 在取消订单前判断订单状态是否可以取消
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static DTO返回值判断 get清单取消判断(String orderId)
        {
            return
                FunctionHelper.JsonDeserialization<DTO返回值判断>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/orderFSA/judgeOrderCancel?orderId=" + orderId
                        )
                    );
        }
        /// <summary>
        /// 修改订单前判断是否可以修改
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static DTO返回值判断 get订单修改判断(String orderId)
        {
            return
                FunctionHelper.JsonDeserialization<DTO返回值判断>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/orderFSA/judgeOrderModify?orderId=" + orderId
                        )
                    );
        }
        /// <summary>
        /// 
        /// </summary>
        private void 订单状态()
        {
            foreach (orderDTO orderDTO in this.orderDTOs)
            {
                if (!String.IsNullOrWhiteSpace(orderDTO.styleSizeCd))
                {
                    try { orderDTO.styleSizeCd = orderDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                if (orderDTO.orderStatus.Equals(Const订单状态.NOT_SCHEDULED))
                {
                    orderDTO.orderStatusName = "未排产";
                }
                else if (orderDTO.orderStatus.Equals(Const订单状态.SCHEDULED))
                {
                    orderDTO.orderStatusName = "已排产";
                }
                else if (orderDTO.orderStatus.Equals(Const订单状态.WIP))
                {
                    orderDTO.orderStatusName = "在制品";
                }
                else if (orderDTO.orderStatus.Equals(Const订单状态.DONE))
                {
                    orderDTO.orderStatusName = "完成";
                }
                else if (orderDTO.orderStatus.Equals(Const订单状态.CANCEL))
                {
                    orderDTO.orderStatusName = "取消";
                }
            }
        }

        /// <summary>
        /// 取得返修一览
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<ReworkIndexDTO> getReworkIndexAll(String shopId, String pageIndex, String pageSize)
        {
            return FunctionHelper.JsonDeserialization<List<ReworkIndexDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/rework/getReworkIndexByShopId?shopId=" + shopId + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize
                        )
                    );
        }
        /// <summary>
        /// 取得返修一览
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<ReworkIndexDTO> getReworkIndex(String pageIndex, String pageSize)
        {
            return FunctionHelper.JsonDeserialization<List<ReworkIndexDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/rework/getReworkIndex?pageIndex=" + pageIndex + "&pageSize=" + pageSize
                        )
                    );
        }
        /// <summary>
        /// 新建返修版本
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public static String saveRework(String barcode, String remarks)
        {
            return RestCall.httpGetMethod(
                URL.接口url + "/rework/saveRework?barcode=" + barcode + "&remarks=" + remarks
                );
        }
        /// <summary>
        /// 新建返修版本
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public static String saveReworkOrder(String barcode, String remarks, String mobile)
        {
            return RestCall.httpGetMethod(
                URL.接口url + "/rework/saveReworkOrder?barcode=" + barcode + "&remarks=" + remarks + "&mobile=" + mobile
                );
        }

        /// <summary>
        /// 新建返修版本带销售组织及销售员
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public static String saveReworkOrderStoreAndAssistant(String barcode, String remarks, String mobile, String storeId, String assistantId)
        {
            return RestCall.httpGetMethod(
                URL.接口url + "/rework/saveReworkOrderStoreAndAssistant?barcode=" + barcode + "&remarks=" + remarks + "&mobile=" + mobile + "&storeId=" + storeId + "&assistantId=" + assistantId
                );
        }

        /// <summary>
        /// 新建返修订单区分销售方式
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public static String saveReworkBySalesType(String barcode, String remarks, String mobile, String storeId, String assistantId)
        {
            return
                RestCall.httpGetMethod(
                    URL.接口url + "/rework/saveReworkBySalesType?barcode=" + barcode + "&remarks=" + remarks + "&mobile=" + mobile + "&storeId=" + storeId + "&assistantId=" + assistantId
                );
        }
    }
    public class Const订单状态
    {
        public const String NOT_SCHEDULED = "NOT_SCHEDULED";//  未排产
        public const String SCHEDULED = "SCHEDULED"; //  已排产
        public const String WIP = "WIP"; //  在制品
        public const String DONE = "DONE"; //  完成
        public const String CANCEL = "CANCEL"; //  取消
        public const String INVALID = "INVALID"; //  不存在

    }


    //orderDTO 获取DTO里的属性
    public class orderDTO
    {
        public String orderId { get; set; }
        public String salesType { get; set; }//套号销售方式
        public String salesChannel { get; set; }//套号销售渠道
        public String customerId { get; set; }
        public String customerName { get; set; }//客户名
        public String consignee { get; set; }//收件人
        public String mobile { get; set; }
        public String addressId { get; set; }
        public String addressProvince { get; set; }//省
        public String addressCity { get; set; }//市
        public String addressDistrict { get; set; }//区
        public String addressDetail { get; set; }//详细地址
        public String zipCode { get; set; }//邮编
        public String materialNameCn { get; set; }//面料描述名称
        public String materialCode { get; set; }  //面料号
        public String composition { get; set; }//面料成分
        public String styleSizeCd { get; set; }//尺码
        public String styleNameCn { get; set; }//款式名称
        public String refStyleId { get; set; }  //  标准款id
        public String orderNo { get; set; } //  订单号
        public Double quantity { get; set; }    //  订单数量
        public DateTime orderDate { get; set; }//订单时间
        public String orderStatus { get; set; } //  订单状态    @See top.toptimus.constValue.PMCOrderStatusConsts
        public String orderStatusName { get; set; }//状态名称
        public String shopId { get; set; }
        public String shopName { get; set; }
        public String paymentStatus { get; set; }
        public orderDTO()
        {

        }
    }
    public class DTO返回值判断
    {
        public String errorCode { get; set; }
        public String errorMessage { get; set; }
        public DTO返回值判断()
        {

        }
    }

    public class ReworkIndexDTO
    {
        public String barcode { get; set; }//成衣条码
        public String orderId { get; set; }//订单id
        public String shopId { get; set; }
        public String shopName { get; set; }
        public String styleNameCn { get; set; }
        public String styleSizeCd { get; set; }
        public String materialCode { get; set; }
        public String materialNameCn { get; set; }
        public String composition { get; set; }
        public List<ReworkInfoDTO> reworkInfoDTOS { get; set; } = new List<ReworkInfoDTO>();//返修信息
        public DateTime createDate { get; set; }//创建日期

        public ReworkIndexDTO()
        {

        }
    }
    public class ReworkInfoDTO
    {
        public String id { get; set; }//ES id
        public String orderNo { get; set; }
        public DateTime orderDate { get; set; }
        public String remarks { get; set; }

        public ReworkInfoDTO()
        {

        }
    }
    public class OrderBomQueryResultDTO
    {
        public String styleCode { get; set; }
        public String materialNo { get; set; }
        public Double orderNumber { get; set; }
        public List<EBOMGroupQueryFacadeDTO> ebomGroupQueryFacadeDTOS { get; set; } = new List<EBOMGroupQueryFacadeDTO>();
        public OrderBomQueryResultDTO()
        {

        }
    }
}
